#!/bin/bash
cur_dir=$(pwd)
case_3ir_path=${cur_dir}/cann-open-smoke-cases/features/3_ir/IRBuild/
case_8graphrun_path=${cur_dir}/cann-open-smoke-cases/features/8_graphrun/graph_run/
export ASCEND_INSTALL_PATH=/usr/local/Ascend/ascend-toolkit/latest
cann_home=${ASCEND_INSTALL_PATH}
case_name=("3_ir" "8_graphrun")
source ${cann_home}/bin/setenv.bash
if [ "x${case_name}" == "x" ]; then
  exit 1
fi

if [[ "${case_name[@]}" =~ "3_ir" ]];then
  cd ${case_3ir_path}/data
  python3 data_generate.py
  cd ..
  rm -rf ${case_3ir_path}/out/*
  echo "========Start to compile 3_ir test========"
  cd ${case_3ir_path};timeout 600 make; chmod +x ./out/ir_build
  cd ${case_3ir_path}/out
  if [ ! -e "ir_build" ];then
    echo "FAILED: file not exist" >> ${cur_dir}/result.txt
    exit 1
  fi
  echo "========Start to execute 3_ir test========="
  ./ir_build Ascend910B4 gen | tee 1.log

  if ! (grep -ri "Build Model1 SUCCESS" 1.log); then
    echo "FAILED: build model fail" >> ${cur_dir}/result.txt
  else
    if ! (grep -ri "Save Offline Model1 SUCCESS" 1.log); then
      echo "FAILED: run checkpoint graph fail" >> ${cur_dir}/result.txt
    else
      echo "SUCCESS: case ir_build success" >> ${cur_dir}/result.txt
    fi
  fi
fi

if [[ "${case_name[@]}" =~ "8_graphrun" ]];then
  cd ${case_8graphrun_path}/data
  python3 data_generate.py
  cd ..
  rm -rf ${case_8graphrun_path}/out/*
  echo "========Start compile 8_graphrun test========"
  cd ${case_8graphrun_path};timeout 600 make;chmod +x ./out/graph_run
  cd ${case_8graphrun_path}/out/
  if [ ! -e "graph_run" ];then
    echo "FAILED: file not exist" >> ${cur_dir}/result.txt
    exit 1
  fi
  echo "========Start to execute 8graphrun test========="
  ./graph_run | tee 1.log

  if ! (grep -ri "Session run Init graph success" 1.log); then
    echo "FAILED: run Init graph fail" >> ${cur_dir}/result.txt
  else
    if ! (grep -ri "Session run checkpoint graph success" 1.log); then
      echo "FAILED: run checkpoint graph fail" >> ${cur_dir}/result.txt
    else
      if  ! (grep -ri "Session run convolution graph success" 1.log); then
        echo "FAILED: run convolution graph fail" >> ${cur_dir}/result.txt
      else
        if ! (grep -ri "Finalize ge success" 1.log); then
          echo " FAILED: ge finalize fail" >> ${cur_dir}/result.txt
        else
          echo "SUCCESS: case graph_run success" >> ${cur_dir}/result.txt
        fi
      fi
    fi
  fi
fi
cd ${cur_dir}